package com.xinya.taobaosale.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xinya.taobaosale.R;
import com.xinya.taobaosale.model.LoadState;

/**
 * @ClassName: BaseFragment$
 * @Description: java类作用描述
 * @Author: xinya
 * @CreateDate: 2021/2/1$ 10:55$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerPresenter();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = getRootView(inflater, container);
        FrameLayout baseContainer = rootView.findViewById(R.id.base_container);
        loadStateView(inflater, container, baseContainer);
        switchLoadState(LoadState.NONE);
        initView(rootView);
        initData(rootView);
        return rootView;
    }

    protected View getRootView(@NonNull LayoutInflater inflater,
                               @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_base, container, false);
    }

    public void switchLoadState(@NonNull LoadState state) {
        mSuccessView.setVisibility(state == LoadState.SUCCESS ? View.VISIBLE : View.GONE);
        mLoadingView.setVisibility(state == LoadState.LOADING ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(state == LoadState.ERROR ? View.VISIBLE : View.GONE);
        mEmptyView.setVisibility(state == LoadState.EMPTY ? View.VISIBLE : View.GONE);
    }

    private void loadStateView(LayoutInflater inflater, ViewGroup container, FrameLayout baseContainer) {
        mSuccessView = inflater.inflate(getFragmentLayoutId(), container, false);
        baseContainer.addView(mSuccessView);
        mLoadingView = inflater.inflate(R.layout.fragment_loading, container, false);
        baseContainer.addView(mLoadingView);
        mErrorView = inflater.inflate(R.layout.fragment_load_error, container, false);
        mErrorView.findViewById(R.id.load_error_view).setOnClickListener(v -> {
            if (mLoadErrorOnclickListener == null) {
                return;
            }
            mLoadErrorOnclickListener.onClick(v);
        });
        baseContainer.addView(mErrorView);
        mEmptyView = inflater.inflate(R.layout.fragment_load_empty, container, false);
        baseContainer.addView(mEmptyView);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegisterPresenter();
    }

    private LoadErrorOnclickListener mLoadErrorOnclickListener;

    public void setLoadErrorOnclickListener(LoadErrorOnclickListener loadErrorOnclickListener) {
        mLoadErrorOnclickListener = loadErrorOnclickListener;
    }

    public interface LoadErrorOnclickListener {
        void onClick(View view);
    }


    protected abstract int getFragmentLayoutId();

    protected abstract void initView(View rootView);

    protected abstract void initData(View rootView);

    protected abstract void registerPresenter();

    protected abstract void unRegisterPresenter();
}
